import log from 'loglevel'  

function sendLogToServer(logEntry:any) {
    fetch('http://localhost:3000/api/logs', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify(logEntry)
    }).catch(err => {
        console.error('Failed to send log to server:', err);
    });
}

log.methodFactory = function(logLevelName, levelNum, loggerName) {
    return function(...args) {
        const logEntry = {
            timestamp: (new Date((new Date()).getTime() + 8 * 3600000)).toISOString().replace('T', ' ').split('Z')[0],
            level: logLevelName,
            message: args.join(' ')
        }

        sendLogToServer(logEntry);
    };
}

log.setLevel('info');

// 导出日志实例
export default log;